#!/bin/sh

echo "Make uImage with DTB ..."

## Check Parameter
if [  $# != 1 ]; then
	echo "mkImageWithDTB.sh [prj name]"
	echo "sample : ./scripts/mkImageWithDTB.sh fh8833"
	exit 1
fi

C_DIR=`dirname $0`
PROJECT=$1

## Setup enviromnent according to the project
case ${PROJECT} in
	fh8626v100)
		export ARCH=arm
		if [ -z "${CROSS_COMPILE}" ]; then
			export CROSS_COMPILE=arm-fullhanv2-linux-uclibcgnueabi-
		fi
	;;
	fh8856)
		export ARCH=arm
		if [ -z "${CROSS_COMPILE}" ]; then
			export CROSS_COMPILE=arm-fullhan-linux-uclibcgnueabi-
		fi
	;;
	fh885xv200)
		export ARCH=arm
		if [ -z "${CROSS_COMPILE}" ]; then
			export CROSS_COMPILE=arm-fullhanv3-linux-uclibcgnueabi-
		fi
	;;
	fh8656)
		export ARCH=arm
		if [ -z "${CROSS_COMPILE}" ]; then
			export CROSS_COMPILE=arm-fullhanv3-linux-uclibcgnueabi-
		fi
	;;
	fh8652)
			export ARCH=arm
			if [ -z "${CROSS_COMPILE}" ]; then
					export CROSS_COMPILE=arm-fullhanv3-linux-uclibcgnueabi-
			fi
	;;
	fhxgm)
			export ARCH=arm
			if [ -z "${CROSS_COMPILE}" ]; then
					export CROSS_COMPILE=arm-fullhanv3-linux-uclibcgnueabi-
			fi
	;;
	*)
		echo "Unknown Project: ${PROJECT}"
		echo "Abort"
		exit 1
	;;
esac
echo "Project: ${PROJECT}"
if [ -e .curprj ]; then
	LASTPRJ=`cat .curprj`
	echo "last project : ${LASTPRJ} "
	if [ "${LASTPRJ}" !=  ${PROJECT} ]; then
		rm -rvf .${PROJECT}config
	fi
fi
echo "${PROJECT}">.curprj

if [ -e ".${PROJECT}config" ]; then
	newer=`find "./arch/arm/configs/${PROJECT}_of_defconfig" -newer ".${PROJECT}config"`
	if [ "$newer" = "./arch/arm/configs/${PROJECT}_of_defconfig" ]; then
		touch .${PROJECT}config
		make ${PROJECT}_of_defconfig
	fi
else
	touch .${PROJECT}config
	make ${PROJECT}_of_defconfig
fi

## Build uImage
echo "Compile U-Image"
rm -vfr ${C_DIR}/../arch/arm/boot/uImage
rm -vfr ${C_DIR}/../arch/arm/boot/uImage_${PROJECT}
make uImage -j10
if [ ! -f ${C_DIR}/../arch/arm/boot/uImage ]; then
	echo "compiled code error, please check"
	exit 1
fi

## Build DTB
echo "Compile dtb"
rm -rfv ${C_DIR}/../arch/arm/boot/dts/${PROJECT}.dtb
make dtbs
if [ ! -f ${C_DIR}/../arch/arm/boot/dts/${PROJECT}.dtb ]; then
	echo "Compiled dts error, please check"
	exit 1
fi

## Make Image with DTB
cat ${C_DIR}/../arch/arm/boot/uImage ${C_DIR}/../arch/arm/boot/dts/${PROJECT}.dtb > ${C_DIR}/../arch/arm/boot/uImage_${PROJECT}
cp ${C_DIR}/../arch/arm/boot/uImage_${PROJECT} ${C_DIR}/../arch/arm/boot/uImage



